<script>
	var department_id = "<?php echo $department_id ?>";	
	function reloadPage(){		
		$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");
	}
	
	function refreshGrid(){
		$("#tblData").jqGrid("setGridParam",{
				postData:{
					"kelas_tahun_ajaran_id" :$("#kelas_tahun_ajaran_id").val(),
				},
				datatype:"json"
			}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");		
	}
	
	function departmentOnChange(){
		window.parent.showLoader();		
		$filter = {"is_active":1,"department_id":$("#department_id").val()};
		$.ajax({	
			url : "index.php?r=data_select/selectDataAjxResult",
			type:"GET",
			async:false,
			//data: {filter: "is_active=true and department_id = '" + $("#department_id").val() + "' union select id,name,1,1,null,null,1,1,1 from m_kelas" },
			data: {obj:"MTahunAjaran",filter: $filter,value_field:"id",title_field:"name"},
			success: function(data) {
				printConsole(data);
				fillSelectData("#tahun_ajaran_id",data);
				tahunAjaranOnChange();	
							
			},
			complete:function(){
				window.parent.hideLoader();
			}
		});		
	}
	
	function tahunAjaranOnChange(){
		//alert("TahunAjaran on Change");
		window.parent.showLoader();		
		$filter = {"is_active":1,"tahun_ajaran_id":$("#tahun_ajaran_id").val()};
		$.ajax({	
			url : "index.php?r=data_select/selectDataAjxResult",
			type:"GET",
			async:false,
			//data: {filter: "is_active=true and department_id = '" + $("#department_id").val() + "' union select id,name,1,1,null,null,1,1,1 from m_kelas" },
			data: {obj:"MKelasTahunAjaran",filter: $filter,value_field:"id",title_field:"kelas.name"},
			success: function(data) {
				printConsole(data);
				fillSelectData("#kelas_tahun_ajaran_id",data);	
				refreshGrid();
			},
			complete:function(){
				window.parent.hideLoader();
			}
		});		
	}
	
	function editRow(id){		
		var url="index.php?r=keuangan/tu/monthly_fee/form&op=edit&id=" + id;
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = true,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
	function deleteRow(id){
		if (confirm("Delete data ?")){			
			window.parent.showLoader();			
			var data = $("#tblData").jqGrid("getRowData",id);
			var editUrl = $("#tblData").jqGrid("getGridParam","editUrl");			
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "del",id:data.id},
				success: function(data) {
					if(data.actionStatus == 0){
						$("#tblData").jqGrid("delRowData",id);			
					}else if(data.actionStatus == 1){
						alert(data.actionMessage);
						printConsole(data.actionError);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			
		}
	}
	function printData(){
		var url="index.php?r=akMaster_Report/department";
		var winName = "Master Department";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=500,height=500;
		var replace = false,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();		
	}

	
	
	$(function(){		
		$("#tblData").jqGrid({
			url:'index.php?r=keuangan/tu/monthly_fee/detailList',
			editUrl:'index.php?r=akademik/pendaftaran_kelas/kelas_tahun_ajaran/edit',
			postData:{"kelas_tahun_ajaran_id":'<?php echo $kelas_tahun_ajaran_id;?>'},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			colNames: ['id','SID','Name','monthly_fee','disc_monthly_fee', 'Keterangan', 'Status','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'sid', index: 'sid', width: 40,search:true},
				{name: 'name', index: 'name', width: 40,search:true},
				{name: 'monthly_fee', index: 'monthly_fee', width: 40,template:moneyTemplate},
				{name: 'disc_monthly_fee', index: 'disc_monthly_fee', width: 40,template:moneyTemplate},
				{name: 'notes', index: 'notes'},								
				{name: 'is_active', index: 'is_active', width: 30, align: 'center', formatter: 'checkbox'},
				{name: 'act', index: 'act', width: 40, align: 'center'},				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Department',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(" + cl + ")' />"; 
					btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit+btnDelete}) 
				} 
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["nama","description"]);
	});
</script>

<div style="">
	
	<div>
		<label class="form mandatory" style="width:100px"><?php echo Yii::t("label", "Departemen") ?></label>	
		<select name="department_id" id="department_id" onChange="departmentOnChange()">
			<?php
				foreach ($departments as $mDepartment){
					$selected = $department_id == $mDepartment["id"] ? "selected" : "";				
					echo"<option value='$mDepartment[id]' $selected>$mDepartment[name]</option>";
				}
			?>
		</select>
	</div>
	<div>
		<label class="form mandatory" style="width:100px"><?php echo Yii::t("label", "Tahun Ajaran") ?></label>	
		<select name="tahun_ajaran_id" id="tahun_ajaran_id" onChange="tahunAjaranOnChange()">
			<?php
				foreach ($tahunAjarans as $tahunAjaran){
					$selected = $tahun_ajaran_id == $tahunAjaran["id"] ? "selected" : "";				
					echo"<option value='$tahunAjaran[id]' $selected>$tahunAjaran[name]</option>";
				}
			?>
		</select>
	</div>
	
	<div>
		<label class="form mandatory" style="width:100px"><?php echo Yii::t("label", "Kelas") ?></label>	
		<select name="kelas_tahun_ajaran_id" id="kelas_tahun_ajaran_id" onChange="refreshGrid()">
			<?php
				foreach ($arrKelas as $kelas){
					$selected = $kelas_tahun_ajaran_id == $kelas["id"] ? "selected" : "";				
					echo"<option value='$kelas[id]' $selected>".$kelas->kelas["name"]."</option>";
				}
			?>
		</select>
	</div>
	
	<div style="float:rigth">	
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => Yii::t("menu","Segarkan"),
						'icon' => "images/action/Action-Refresh-icon.png",
						'callback'=>"reloadPage"
					),										
					array(
						'id' => "btnPrint",
						'title' => Yii::t("menu","Cetak"),
						'icon' => "images/action/Actions-document-print-icon2.png",
						'callback'=>"printData"
					),					
					
				)
			)
		);
	?>
	</div>	
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
</div>